package com.jd.jr.insights.dm.dim.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.jd.jr.insights.dm.dim.dao.BizDao;
import com.jd.jr.insights.dm.dim.dao.impl.BizDaoImpl;
import com.jd.jr.insights.dm.dim.domain.Business;
import com.jd.jr.insights.dm.dim.service.BizService;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.Result;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.ResultCode;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.Tree;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guoxuefei1 on 2017/7/18.
 */
public class BizServiceImpl implements BizService {
    BizDao bizDao = new BizDaoImpl();

    public String getBizListJson() {
        Tree tree = new Tree(bizDao.getList());
        TreeNode treeNode = tree.getTreeNode("2");

        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); // 构造方法里，也可以直接传需要序列化的属性名字
        filter.getExcludes().add("parent");
        filter.getExcludes().add("allChildren");
        List<TreeNode> list=new ArrayList<TreeNode>();
        list.add(treeNode);
       return JSONObject.toJSONString(list, filter);
        //return JSON.toJSONString(bizDao.getList());
    }


    public String getBizListJsonNm() {
        return JSON.toJSONString(bizDao.getList());
    }

    public String deleteById(Business biz) {
        if(bizDao.deleteById(biz)==true){
            return JSON.toJSONString(new Result(ResultCode.OK));
        }
        return JSON.toJSONString(new Result(ResultCode.ERROT));
    }

    public String save(Business biz) {
        if(bizDao.save(biz)==true){
            return JSON.toJSONString(new Result(ResultCode.OK));
        }
        return JSON.toJSONString(new Result(ResultCode.ERROT));
    }


    public String update(Business biz) {
        if(bizDao.update(biz)==true){
            return JSON.toJSONString(new Result(ResultCode.OK));
        }
        return JSON.toJSONString(new Result(ResultCode.ERROT));
    }


}
